package leetcodev1.数组;

public class LeetCode73 {

    public void setZeroes(int[][] matrix) {
        int m = matrix.length, n = matrix[0].length;
        boolean[] row = new boolean[m];
        boolean[] col = new boolean[n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (matrix[i][j] == 0) {
                    row[i] = col[j] = true;
                }
            }
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (row[i] || col[j]) {
                    matrix[i][j] = 0;
                }
            }
        }
    }

    //暴力解
    //第一遍遍历把带0的行和列统计处理
    //第二遍进行处理
    public void setZeroes2(int[][] matrix) {
        int row = matrix.length, column = matrix[0].length;
        for (int i = 1; i < row; i++) {
            for (int j = 1; j < column; j++) {
                if (matrix[i - 1][j] == 0) {
                    matrix[i][j] = 0;
                    continue;
                }

                if (matrix[i][j - 1] == 0) {
                    matrix[i][j] = 0;
                    continue;
                }

                //回溯 bug
                if (matrix[i][j] == 0) {
                    int tmpI = i, tmpJ = j;
                    while (tmpI >= 0 && matrix[tmpI][tmpJ] != 0) {
                        matrix[tmpI--][tmpJ] = 0;
                    }

                    while (tmpJ >= 0 && matrix[tmpI][tmpJ] != 0) {
                        matrix[tmpI][tmpJ--] = 0;
                    }
                }
            }
        }
    }
}
